package com.example.animation;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

public class AttributeAnimation extends AppCompatActivity {
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attribute_animation);
        initOtherView();
        imageView = (ImageView) findViewById(R.id.image);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(AttributeAnimation.this, "image被点击了", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initOtherView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    //缩放动画
    public void attributeAnimation(View view) {
//        ObjectAnimator objectAnimation = ObjectAnimator.ofFloat(imageView,"scaleX",1f,2f);
        ObjectAnimator objectAnimation = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f);
        objectAnimation.setDuration(300);
        objectAnimation.start();
    }

    //平移动画
    public void complementAnimation(View view) {
//        ObjectAnimator objectAnimatior = ObjectAnimator.ofFloat(imageView, "translationY", 0, 200);
////        ObjectAnimator objectAnimatior = ObjectAnimator.ofFloat(imageView,"translationX",0,200);
//        objectAnimatior.setDuration(300);
//        objectAnimatior.start();
        /**
         * ObjectAnimator 不存在差值器
         */

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,200);
        valueAnimator.setTarget(imageView);
        valueAnimator.setInterpolator(new MyTimeInterpolator());
        valueAnimator.setDuration(300).start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                imageView.setTranslationX((Float) valueAnimator.getAnimatedValue());
            }
        });
    }

    //透明度动画
    public void frameAnimation(View view) {
        ObjectAnimator objectAnimatior = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f);

        objectAnimatior.setDuration(500);
        objectAnimatior.start();
    }

    //组合动画
    public void animationSet(View view) {
        //第一张实现方法
//        PropertyValuesHolder objectAnimation = PropertyValuesHolder.ofFloat("scaleX",1f,2f);
//        PropertyValuesHolder objectAnimation1 = PropertyValuesHolder.ofFloat("scaleY",1f,2f);
//        PropertyValuesHolder objectAnimator = PropertyValuesHolder.ofFloat("rotationY",0f,180f);//绕着Y周旋转，0f起始角度，180f最后角度
//        PropertyValuesHolder objectAnimator2 = PropertyValuesHolder.ofFloat("rotationX",0f,180f);
//        ObjectAnimator.ofPropertyValuesHolder(imageView,objectAnimation,objectAnimation1,objectAnimator,objectAnimator2).setDuration(500).start();


        //第二种实现方法
        ObjectAnimator objectAnimation = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f);
        ObjectAnimator objectAnimation1 = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotationY", 0f, 180f);//绕着Y周旋转，0f起始角度，180f最后角度
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(imageView, "rotationX", 0f, 180f);
        AnimatorSet set = new AnimatorSet();
        //同时执行
//        set.playSequentially(objectAnimation,objectAnimation1,objectAnimator,objectAnimator2);
        /**
         * objectAnimation，objectAnimation1,objectAnimator
         * objectAnimator2接着执行
         */
        set.play(objectAnimation).with(objectAnimation1);
        set.play(objectAnimation1).with(objectAnimator);
        set.play(objectAnimator2).after(objectAnimator);
        set.setDuration(500);
        set.start();
    }

    //旋转动画
    public void rotationAnimation(View view) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotationY", 0f, 180f);//绕着Y周旋转，0f起始角度，180f最后角度
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView,"rotationX",0f,180f);
        objectAnimator.setDuration(300);
        objectAnimator.start();
    }

    public void viewPropertyAnimator(View view){
        ViewPropertyAnimator viewpro = imageView.animate();
        viewpro.translationX(0f);
        viewpro.translationXBy(200f);
        viewpro.setDuration(500);
        viewpro.start();
    }

}
